package com.jprocms.module.cms.dal.mysql.accesstrend;

import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.mybatis.core.mapper.BaseMapperX;
import com.jpro.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.jprocms.module.cms.controller.admin.accesstrend.vo.AccessTrendListReqVO;
import com.jprocms.module.cms.controller.admin.accesstrend.vo.AccessTrendPageReqVO;
import com.jprocms.module.cms.dal.dataobject.accesstrend.AccessTrendDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 访问统计趋势 Mapper
 *
 * @author jprocms
 */
@Mapper
public interface AccessTrendMapper extends BaseMapperX<AccessTrendDO> {

    default PageResult<AccessTrendDO> selectPage(AccessTrendPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AccessTrendDO>()
                .eqIfPresent(AccessTrendDO::getSiteId, reqVO.getSiteId())
                .eqIfPresent(AccessTrendDO::getPvCount, reqVO.getPvCount())
                .eqIfPresent(AccessTrendDO::getUvCount, reqVO.getUvCount())
                .eqIfPresent(AccessTrendDO::getIpCount, reqVO.getIpCount())
                .eqIfPresent(AccessTrendDO::getNvCount, reqVO.getNvCount())
                .eqIfPresent(AccessTrendDO::getBounceCount, reqVO.getBounceCount())
                .eqIfPresent(AccessTrendDO::getAccessDuration, reqVO.getAccessDuration())
                .eqIfPresent(AccessTrendDO::getDateString, reqVO.getDateString())
                .betweenIfPresent(AccessTrendDO::getDateString, reqVO.getDateBeginString(), reqVO.getDateEndString())
                .eqIfPresent(AccessTrendDO::getStatPeriod, reqVO.getStatPeriod())
                .orderByAsc(AccessTrendDO::getDateString));
    }

    default List<AccessTrendDO> selectList(AccessTrendListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<AccessTrendDO>()
                .eqIfPresent(AccessTrendDO::getSiteId, reqVO.getSiteId())
                .eqIfPresent(AccessTrendDO::getPvCount, reqVO.getPvCount())
                .eqIfPresent(AccessTrendDO::getUvCount, reqVO.getUvCount())
                .eqIfPresent(AccessTrendDO::getIpCount, reqVO.getIpCount())
                .eqIfPresent(AccessTrendDO::getNvCount, reqVO.getNvCount())
                .eqIfPresent(AccessTrendDO::getBounceCount, reqVO.getBounceCount())
                .eqIfPresent(AccessTrendDO::getAccessDuration, reqVO.getAccessDuration())
                .eqIfPresent(AccessTrendDO::getDateString, reqVO.getDateString())
                .betweenIfPresent(AccessTrendDO::getDateString, reqVO.getDateBeginString(), reqVO.getDateEndString())
                .eqIfPresent(AccessTrendDO::getStatPeriod, reqVO.getStatPeriod())
                .orderByAsc(AccessTrendDO::getDateString));
    }

    /**
     * 删除指定日期字符串之前的数据
     *
     * @param dateString 指定的日期字符串
     * @param period     统计周期
     * @return 被删除的数据条数
     */
    int deleteBeforeDateString(@Param("dateString") String dateString, @Param("period") short period);

    /**
     * 根据日期字符串删除数据
     *
     * @param dateString 日期字符串
     * @return 被删除的数据条数
     */
    int deleteByDateString(@Param("dateString") String dateString);

    /**
     * 根据站点ID删除数据
     *
     * @param siteId 站点ID
     * @return 被删除的数据条数
     */
    int deleteBySiteId(@Param("siteId") Integer siteId);

    /**
     * 按日期统计
     *
     * @param period 统计周期
     * @param begin  开始日期
     * @param end    介绍日期
     * @return 统计结果
     */
    List<AccessTrendDO> statByDate(@Param("period") Short period, @Param("begin") String begin, @Param("end") String end);

}
